<?php

namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Material
 *
 * @ORM\Table(name="material")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\MaterialRepository")
 */
class Material
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="text")
     */
    private $description;

    /**
     * @var string
     *
     * @ORM\Column(name="unique_code", type="string", length=128, unique=true)
     */
    private $uniqueCode;
    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="MaterialOrder", mappedBy="material")
     */
    private $orders;

    /**
     * @return ArrayCollection
     */
    public function getOrders()
    {
        return $this->orders;
    }

    /**
     * @param ArrayCollection $orders
     */
    public function setOrders($orders)
    {
        $this->orders = $orders;
    }


    /**
     * @var float
     *
     * @ORM\Column(name="price", type="decimal", precision=10, scale=2)
     */
    private $price;

    /**
     * @return float
     */
    public function getPrice(): float
    {
        return (float)$this->price;
    }

    /**
     * @param float $price
     */
    public function setPrice($price)
    {
        $this->price = (float)$price;
    }

    /**
     * @var int
     *
     * @ORM\Column(name="inventory", type="integer")
     */
    private $inventory;
    /**
     * @var \DateTime
     *
     * @ORM\Column(name="add_time", type="datetime")
     */
    private $addTime;
    /**
     * That means this material is removed if the value is greater than 0.
     * @var int
     * @ORM\Column(name="remove", type="smallint", options={"default"=0})
     */
    private $remove;

    /**
     * @return int
     */
    public function getRemove(): int
    {
        return $this->remove;
    }

    /**
     * @param int $remove
     */
    public function setRemove(int $remove)
    {
        $this->remove = $remove;
    }

    /**
     * @ORM\ManyToMany(targetEntity="Product", inversedBy="materials")
     * @ORM\JoinTable(name="products_materials",
     *  joinColumns={
     *      @ORM\JoinColumn(name="material_id", referencedColumnName="id")
     *  },
     *  inverseJoinColumns={
     *      @ORM\JoinColumn(name="product_id", referencedColumnName="id")
     *  }
     * )
     */
    private $products;

    public function addProduct(Product $product)
    {
        if ($this->products->contains($product)) {
            return;
        }
        $this->products->add($product);
        $product->addMaterial($this);
    }

    public function removeProduct(Product $product)
    {
        if (!$this->products->contains($product)) {
            return;
        }
        $this->products->removeElement($product);
        $product->removeMaterial($this);
    }

    public function __construct()
    {
        $this->products = new ArrayCollection();
        $this->orders = new ArrayCollection();
    }

    /**
     * @return mixed
     */
    public function getProducts()
    {
        return $this->products;
    }

    /**
     * @param mixed $products
     */
    public function setProducts($products)
    {
        $this->products = $products;
    }


    /**
     * @return \DateTime
     */
    public function getAddTime()
    {
        return $this->addTime;
    }

    /**
     * @param \DateTime $addTime
     * @return Material
     */
    public function setAddTime($addTime)
    {
        $this->addTime = $addTime;
        return $this;
    }

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Material
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set description
     *
     * @param string $description
     *
     * @return Material
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Set uniqueCode
     *
     * @param string $uniqueCode
     *
     * @return Material
     */
    public function setUniqueCode($uniqueCode)
    {
        $this->uniqueCode = $uniqueCode;

        return $this;
    }

    /**
     * Get uniqueCode
     *
     * @return string
     */
    public function getUniqueCode()
    {
        return $this->uniqueCode;
    }


    /**
     * Set inventory
     *
     * @param integer $inventory
     *
     * @return Material
     */
    public function setInventory($inventory)
    {
        $this->inventory = (int)$inventory;

        return $this;
    }

    /**
     * Get inventory
     *
     * @return int
     */
    public function getInventory()
    {
        return (int)$this->inventory;
    }

}

